package net.homeftp.heimo;

import java.util.ArrayList;
import java.util.List;

import net.homeftp.heimo.adaptor.ListItemAdaptor;
import net.homeftp.heimo.model.ListItem;
import net.homeftp.heimo.task.GetDataTask;
import net.homeftp.heimo.task.ITaskListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;
/*
 * 
 * Need to import the source project.
 * Cannot reference the com.markupartist.android.widget
 * Library from JAR
 * */
public class PullToRefreshActivity extends Activity implements ITaskListener {
	private PullToRefreshListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_layout_pull_refresh);
		final ITaskListener i = this;
		listView = (PullToRefreshListView) findViewById(R.id.listview);
		fnAddItems(1);
		listView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				// Do work to refresh the list here.
				new GetDataTask(i).execute();
			}
		});
	}

	private void fnAddItems(int z) {
		List<ListItem> list = new ArrayList<ListItem>();
		ListItemAdaptor adp = new ListItemAdaptor(this, list);
		listView.setAdapter(adp);
		for (int i = 0; i < z; i++) {
			ListItem li = new ListItem();
			li.setImageID(R.drawable.ic_launcher);
			li.setTitle("Item No : " + i);
			li.setDescription("This is testing item,\n with item no :" + i);
			li.setRemark("$" + (i * 25));
			adp.addItems(li);
		}
		adp.update();
	}

	@Override
	public void onTaskCompleted(Object o) {
		fnAddItems(50);
		listView.onRefreshComplete();
		Toast.makeText(this, "onTaskCompleted!", Toast.LENGTH_LONG).show();
	}

	@Override
	public void onTaskFailed(Object o) {
		Toast.makeText(this, "onTaskFailed!", Toast.LENGTH_LONG).show();
	}
}